package com.chixing.day15.day15_Home;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class ReadExcelToList {
    public static void main(String[] args) throws IOException {
        List<Student> list = new ArrayList<>();
        XSSFWorkbook wb = new XSSFWorkbook("//Users//jasonmarslyon//CX_Workspace//Java_Workspace//Test//student.xlsx");
        XSSFSheet sheet = wb.getSheetAt(0);
        int rows = sheet.getPhysicalNumberOfRows();//总行数
        for (int i = 1; i < rows; i++) {
            Student stu = new Student();
            Row row = sheet.getRow(i);

            stu.setStuNo((int)row.getCell(0).getNumericCellValue());
            stu.setStuName(row.getCell(1).getStringCellValue());
            stu.setStuGender(row.getCell(2).getStringCellValue());
            stu.setStuMajor(row.getCell(3).getStringCellValue());
            stu.setStuBirthday(StringLocalDate(row.getCell(4).getStringCellValue()));
            list.add(stu);
            System.out.println(stu);
        }

    }

    private static LocalDate StringLocalDate(String str) {
        LocalDate date = null;
        if(str.length() == 8){
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.M.d");
            date = LocalDate.parse(str,formatter);
        }
        if(str.length() == 9){
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.M.dd");
            date = LocalDate.parse(str,formatter);
        }
        return date;
    }

}

